#include <QtCore/qdir.h>
#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "adbutils.h"
#include "settings.h"
#include "settingdialog.h"
#include "mainworkthread.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    label = new LabelCustom(ui->centralwidget);
    QPixmap map;
    map.load(":img/resource/logo.jpg");
    label->setPixmap(map);
    timer = new QTimer(label);
    connect(timer, SIGNAL(timeout()), this, SLOT(onRefreshCapTime()));
    ui->verticalLayout->insertWidget(0, label);
    connect(label, SIGNAL(onMouseEvent(int,QMouseEvent*)), this, SLOT(onMouseEventNotify(int,QMouseEvent*)));

    MainWorkThread *work = MainWorkThread::getInstance();
    connect(work, SIGNAL(statusChangeSignal(int, int, int, int)), this, SLOT(onStatusChange(int, int, int, int)));
    startMini();
}

MainWindow::~MainWindow()
{
    delete ui;
    delete timer;
    delete label;
}

bool MainWindow::startMini() {
    MainWorkThread *work = MainWorkThread::getInstance();
    return work->startMini(MODE_CAPTURE | MODE_TOUCH);
}

void MainWindow::onMouseEventNotify(int type, QMouseEvent* ev) {
   qDebug() << "onMouseEventNotify" << type << ev;
}

void MainWindow::showSettings() {
    SettingDialog sDialog;
    if (sDialog.exec()) {

    }
}

void MainWindow::onStatusChange(int mode, int cap_state, int touch_state, int ret) {
    if (ret == 0) {
        if (mode == MODE_CAPTURE)
            timer->start();
    } else {
        if (mode == MODE_CAPTURE)
            timer->stop();
    }
    QString msg;
    if (ret < 0) {
       if (ret == -1)
           msg = "ERROR: device not connect or connect more then one!";
       else
           msg = "ERROR: cannot get device info";
     } else {
        if (cap_state == STATUS_CONNECTED)
           msg = msg + "connected capture; ";
        else if (cap_state == STATUS_CONNECTING)
           msg = msg + "connecting capture; ";
        else if ( cap_state == STATUS_DISCONNECT)
           msg = msg + "disconnected capture; ";
        if (touch_state == STATUS_CONNECTED)
           msg = msg + "connected touch; ";
        else if ( touch_state == STATUS_CONNECTING)
           msg = msg + "connecting touch; ";
        if (touch_state == STATUS_DISCONNECT)
           msg = msg + "disconnected touch; ";
     }
     ui->statusbar->showMessage(msg);
}

void MainWindow::onRefreshCapTime() {
//    if self.mini_cap is not None:
//        ret, frame = self.mini_cap.get_capture()
//        if ret is False:
//            return
//        # The width of the image width must alignment with 4 times
//        __width = frame.shape[1] / 4
//        if __width > 0:
//            __image_resize = frame[:, 0:__width * 4]
//            __image_resize = cv2.cvtColor(__image_resize, cv2.COLOR_BGR2RGB)
//            capture = QtGui.QImage(__image_resize.data, __image_resize.shape[1],
//                                   __image_resize.shape[0], QtGui.QImage.Format_RGB888)
//            self.label.resize(capture.width(), capture.height())
//            self.label.setPixmap(QtGui.QPixmap.fromImage(capture))
//            self.label.show()
}

void MainWindow::onActionTriggered(QAction *action) {
    if (action == ui->actionConnect) {

    } else if (action == ui->actionSetting){
        showSettings();
    }
}

void MainWindow::onPowerPress() {}
void MainWindow::onPowerRelease() {
    AdbUtils::sendKey(AdbUtils::KeyCode::power);
}

void MainWindow::onHomePress() {}
void MainWindow::onHomeRelease() {
     AdbUtils::sendKey(AdbUtils::KeyCode::home);
}

void MainWindow::onMenuPress() {}
void MainWindow::onMenuRelease() {
    AdbUtils::sendKey(AdbUtils::KeyCode::menu);
}

void MainWindow::onRecentsPress() {}
void MainWindow::onRecentsRelease() {
    AdbUtils::sendKey(AdbUtils::KeyCode::app_switch);
}

void MainWindow::onBackPress() {}
void MainWindow::onBackRelease() {
    AdbUtils::sendKey(AdbUtils::KeyCode::back);
}

void MainWindow::onVolIncPress() {}
void MainWindow::onVolIncRelease() {
    AdbUtils::sendKey(AdbUtils::KeyCode::vol_up);
}

void MainWindow::onVolDecPress() {}
void MainWindow::onVolDecRelease(){
    AdbUtils::sendKey(AdbUtils::KeyCode::vol_down);
}

void MainWindow::onSendClick(){}
